package com.googlecode.bip.xml.export;

import java.sql.SQLException;
import java.util.List;
import javax.naming.NamingException;

/**
 * TODO: doc
 *
 * @author Claude CHOISNET
 */
//not public
class XMLValueDescriptionsFactory
    extends XMLSurveyEntryFactoryBuilder<XMLValueDescriptions>
{
    private static final long serialVersionUID = 1L;

    protected XMLValueDescriptionsFactory(
            XMLSurveyConnection connection,
            String              sql
            )
    {
        super( connection, sql );
    }

    /**
     * TODO: doc
     *
     * @param xmlQuestion
     * @return
     * @throws SQLException
     * @throws NamingException
     */
    public static List<XMLValueDescriptions> createList(
            XMLSurveyConnection connection,
            XMLQuestion         xmlQuestion
            )
        throws SQLException, NamingException
    {
        StringBuilder sql = new StringBuilder();
        sql.append( "SELECT * FROM `questionvalues_local` WHERE `idquestion`=" )
           .append( Integer.toString( xmlQuestion.getIdQuestion() ) )
           .append( ';' );
        XMLValueDescriptionsFactory xml = new XMLValueDescriptionsFactory( connection, sql.toString() );
        //ResultSet rset = xml.execute();
        XMLSurveyConnection.ResultSetCache rset = xml.execute(
                "value",
                "valuedescription"
                );

        try {
            while( rset.next() ) {
                int     value               = rset.getInt( "value" );
                String  valuedescription    = rset.getString( "valuedescription" );

                xml.add( new XMLValueDescriptions( value, valuedescription ) );
                }
            }
        finally {
            rset.close();
            }

        return xml.getList();
    }
}
